package cn.daniellee.plugin.wc.listener;

import cn.daniellee.plugin.wc.Welcome;
import cn.daniellee.plugin.wc.core.WelcomeCore;
import cn.daniellee.plugin.wc.model.PlayerData;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerJoinEvent;

import java.util.Collections;

public class PlayerListener implements Listener {

	@EventHandler
	public void onPlayerJoin(PlayerJoinEvent e) {
		WelcomeCore.handlePlayerJoin(e.getPlayer());
	}

	@EventHandler
	public void onPlayerChat(AsyncPlayerChatEvent e) {
		if (e.getMessage().startsWith("▶")) return;
		// 检查是否包含关键字
		for (String keyword : Welcome.getInstance().getKeywords()) {
			if (e.getMessage().contains(keyword)) {
				break;
			}
			return;
		}
		// 检查是否包含新玩家名
		for (String name : Collections.list(WelcomeCore.newPlayerCache.keys())) {
			if (e.getMessage().contains(name)) {
				WelcomeCore.handleWelcome(e.getPlayer(), name, false);
				break;
			}
		}
	}

}
